package com.jxau.mall.user.service.impl;

import com.jxau.mall.user.entity.UserInfo;
import com.jxau.mall.user.entity.UserLoginLog;
import com.jxau.mall.user.service.ILoginService;
import com.jxau.mall.user.service.IUserLoginLogService;
import com.jxau.mall.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.time.LocalDateTime;

/**
 * @program: shop
 * @description: 用户登录业务的实现类
 * @author: guojiani
 * @create: 2025-04-24 19:50
 **/
@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserLoginLogService userLoginLogService;
    @Override
    public UserInfo login(String username, String password) throws Exception {
        // 登录
        UserInfo userInfo = userService.login(username, password);
        // 登录成功后写日志
        if(userInfo != null){
            InetAddress inetAddress = InetAddress.getLocalHost();
            String ip = inetAddress.getHostAddress();
            UserLoginLog log = new UserLoginLog(
                    System.currentTimeMillis(),
                    userInfo.getId(),
                    LocalDateTime.now(),
                    ip);
            userLoginLogService.save(log);
            return userInfo;
        }
        return null;
    }
}
